package cii_lib_graph;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cii_lib.CCII_CONFIG_TABLE;
import cii_lib.CIIlib.TABLE_ACCESS;

public class GAccessPanel extends javax.swing.JPanel {

	private static final Logger log = LoggerFactory
			.getLogger(GAccessPanel.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ButtonGroup buttonGroup1;
	private JRadioButton jIR;
	private JRadioButton jRC;
	private JRadioButton jRW;
	private JRadioButton jWO;
	private JRadioButton jRO;
	private JRadioButton jNA;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new GAccessPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public GAccessPanel() {
		super();
		initGUI();
	}
	
	public GAccessPanel(CCII_CONFIG_TABLE cct) {
		super();
		initGUI();
		updateAccesMode(cct);		
	}
	
	private void initGUI() {
		try {
			
			String panelName = new String(" Access ");

			setPreferredSize(new java.awt.Dimension(217, 84));
			setBounds(602, 560, 217, 84);
			setBorder(BorderFactory.createTitledBorder(null, panelName, TitledBorder.LEADING, TitledBorder.TOP));
			setLayout(null);
			{
				jNA = new JRadioButton();
				add(jNA);
				jNA.setText("NA");
				jNA.setBounds(14, 14, 42, 21);
			}
			{
				jRO = new JRadioButton();
				add(jRO);
				jRO.setText("RO");
				jRO.setBounds(56, 14, 42, 21);
			}
			{
				jWO = new JRadioButton();
				add(jWO);
				jWO.setText("WO");
				jWO.setBounds(105, 14, 49, 21);
			}
			{
				jRW = new JRadioButton();
				add(jRW);
				jRW.setText("RW");
				jRW.setBounds(105, 35, 49, 21);
			}
			{
				jRC = new JRadioButton();
				add(jRC);
				jRC.setText("RC");
				jRC.setBounds(154, 14, 41, 21);
			}
			{
				jIR = new JRadioButton();
				add(jIR);
				jIR.setText("IR");
				jIR.setBounds(56, 35, 42, 21);
			}
			{
				buttonGroup1 = new ButtonGroup();
				buttonGroup1.add(jNA);
				buttonGroup1.add(jRO);
				buttonGroup1.add(jWO);
				buttonGroup1.add(jRW);
				buttonGroup1.add(jRC);
				buttonGroup1.add(jIR);
				
				jNA.setEnabled(false);
				jRO.setEnabled(false);
				jWO.setEnabled(false);
				jRW.setEnabled(false);
				jRC.setEnabled(false);
				jIR.setEnabled(false);
			}
			setVisible(true);
			} catch (Exception e) {
			log.error("", e);
		}
	  
	}
	/**
	 * Set the panel with reference of config table item
	 * @param ctab reference of config table item
	 */
	protected void updateAccesMode(CCII_CONFIG_TABLE ctab) {
		if (ctab.Access == TABLE_ACCESS.RO) {
			jRO.setSelected(true);
		} else
		if (ctab.Access == TABLE_ACCESS.IR) {
			jIR.setSelected(true);
		} else
		if (ctab.Access == TABLE_ACCESS.RC) {
			jRC.setSelected(true);
		} else
		if (ctab.Access == TABLE_ACCESS.WO) {
			jWO.setSelected(true);
		} else
		if (ctab.Access == TABLE_ACCESS.RW) {
			jRW.setSelected(true);
		} else
		{
			jNA.setSelected(true);
		}
	}
	
	public JPanel getJPanel() {
		return this;
	}
	public JPanel getJPanel(CCII_CONFIG_TABLE cct) {
		updateAccesMode(cct);
		return this;
	}
	
	public ButtonGroup getDiodes() {
		return buttonGroup1;
	}

}
